package arraylistdemo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author 12629
 * @Date 2022/3/12 10:35
 * @Description：
 */
class Card {
    private String suit;//花色
    private int rank;//数字

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "花色："+suit+" 数字："+rank;
    }
}

public class CardDemo {
    private static final String[] SUITS = {"♥","♠","♦","♣"};

    public static List<Card> buyCard() {
        List<Card> cards = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
               /* String suit = SUITS[i];
                int rank = j;
                arraylistdemo.Card card = new arraylistdemo.Card(suit,j);
                cards.add(card);*/
                cards.add(new Card(SUITS[i],j));
            }
        }
        return cards;
    }

    public static void shuffle(List<Card> cards) {
        Random random = new Random();
        for (int i = cards.size()-1; i > 0 ; i--) {
            int index = random.nextInt(i);//[0,100)
            //arraylistdemo.Card tmp = cards[i];
            Card tmp = cards.get(i);
            //cards[i] = cards[index];
            cards.set(i,cards.get(index));
            //cards[index] = tmp;
            cards.set(index,tmp);
        }
    }

    public static void main(String[] args) {
        List<Card> cardList = buyCard();
        System.out.println("买牌："+cardList);

        shuffle(cardList);
        System.out.println("洗牌："+cardList);

        System.out.println("揭牌：");

        List<List<Card>> hands = new ArrayList<>();

        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardList.remove(0);//每次都去删除0下标的牌
                hands.get(j).add(card);
            }
        }
        System.out.println("第1个人的牌："+hand1);
        System.out.println("第2个人的牌："+hand2);
        System.out.println("第3个人的牌："+hand3);
        System.out.println("剩下的牌："+cardList);
    }
}
